﻿using EV.EventBus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ZoneLine.Stations;
using ZoneLine.Stations.XWStation;
using ZoneLine.Views.HStationViews;

namespace ZoneLine.Views.XWStationViews
{
    /// <summary>
    /// XWMainView.xaml 的交互逻辑
    /// </summary>
    public partial class XWMainView : Window
    {
        public XWMainView()
        {
            InitializeComponent();
            this.Loaded += XWMainView_Loaded;
        }

        private void XWMainView_Loaded(object sender, RoutedEventArgs e)
        {
            MessageCenter.Subscribe(nameof(XW010SStation) + "uilog", nameof(XWMainView), (s) =>
            {
                XW010SShowMsg(s.Source.Payload);
                return Task.CompletedTask;
            });

            MessageCenter.Subscribe(nameof(XW010LStation) + "uilog", nameof(XWMainView), (s) =>
            {
                XW010LShowMsg(s.Source.Payload);
                return Task.CompletedTask;
            });
            
            MessageCenter.Subscribe(nameof(XW010RStation) + "uilog", nameof(XWMainView), (s) =>
            {
                XW010RShowMsg(s.Source.Payload);
                return Task.CompletedTask;
            });
        }

        private int _xw010SCount = 0;
        private void XW010SShowMsg(object info)
        {
            if (info is string ss)
            {
                Dispatcher.Invoke((Action)(() =>
                {
                    this._xw010SCount++;
                    if (this._xw010SCount > 500)
                    {
                        XW010SLog.Document.Blocks.Clear();
                        this._xw010SCount = 0;
                    }
                    XW010SLog.AppendText(DateTime.Now.ToString("HH:mm:ss") + ss + "\r\n");
                    XW010SLog.ScrollToEnd();
                }));
            }
        }

        private int _xw010LCount = 0;
        private void XW010LShowMsg(object info)
        {
            if (info is string ss)
            {
                Dispatcher.Invoke(() =>
                {
                    _xw010LCount++;
                    if (_xw010LCount > 500)
                    {
                        XW010LLog.Document.Blocks.Clear();
                        _xw010LCount = 0;
                    }
                    XW010LLog.AppendText(DateTime.Now.ToString("HH:mm:ss") + ss + "\r\n");
                    XW010LLog.ScrollToEnd();
                });
            }
        }

        private int _xw010rCount = 0;
        private void XW010RShowMsg(object info)
        {
            if (info is string ss)
            {
                Dispatcher.Invoke(() =>
                {
                    _xw010rCount++;
                    if (_xw010rCount > 500)
                    {
                        XW010RLog.Document.Blocks.Clear();
                        _xw010rCount = 0;
                    }
                    XW010RLog.AppendText(DateTime.Now.ToString("HH:mm:ss") + ss + "\r\n");
                    XW010RLog.ScrollToEnd();
                });
            }
        }

        private void QueryStationResultView(object sender, RoutedEventArgs e)
        {
            //var view = StationResultView.GetView();
            //view.Show();
            var view = new HistoryData();
            view.Show();
        }

        private void BtnOpenConfigView(object sender, RoutedEventArgs e)
        {
            var view = new XWStationConfigView();
            view.Owner = this;
            view.ShowDialog();
        }
    }
}
